package de.sendorian.app.forumArchive.domain;


import static org.apache.commons.lang.StringUtils.*;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import de.sendorian.app.forumArchive.Config;

@Entity
public class Forum {

	@Id
	@GeneratedValue
	private int id;

	private String name;

	private String url;

	@OneToMany(cascade = CascadeType.ALL)
	@Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	@Fetch(FetchMode.SUBSELECT)
	private Set<Topic> topics = new HashSet<Topic>();

	private int pages;


	public Forum() {
	}


	public Forum(String name, String url) {
		setName(name);
		setUrl(url);
	}


	public int getId() {
		return id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		name = name.replace("/", ",").replace("\\", ",");
		this.name = name;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		if (!startsWith(url, "http://")) {
			this.url = Config.get().getBaseUrl() + url;
		} else {
			this.url = url;
		}
	}


	public Set<Topic> getTopics() {
		return topics;
	}


	public void setTopics(Set<Topic> topics) {
		this.topics = topics;
	}


	public int getPages() {
		return pages;
	}


	public void setPages(int pages) {
		this.pages = pages;
	}


	@Override
	public String toString() {
		return "Forum \"" + getName() + "\" (" + getUrl() + ")";
	}

}
